package com.linxz.ui;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.linxz.ui.pojo.PreImageBean;
import com.previewlibrary.GPreviewBuilder;
import com.previewlibrary.ZoomMediaLoader;
import com.previewlibrary.loader.IZoomMediaLoader;
import com.previewlibrary.loader.MySimpleTarget;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Linxz
 * 创建日期：2019年10月20日 00:59
 * version：v4.5.4
 * 描述：图片预览 https://github.com/yangchaojiang/ZoomPreviewPicture
 */
public class ImagePreViewManager {


    public static void preview(Activity activity, List<PreImageBean> imageBeans, int position){
        IZoomMediaLoader zoomMediaLoader=new IZoomMediaLoader(){
            @Override
            public void displayImage(@NonNull Fragment context, @NonNull String path, ImageView imageView, @NonNull final MySimpleTarget simpleTarget) {
                Glide.with(context)
                        .asBitmap()
                        .load(path)

                        .listener(new RequestListener<Bitmap>() {
                            @Override
                            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                                simpleTarget.onLoadFailed(null);
                                return false;
                            }

                            @Override
                            public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                                simpleTarget.onResourceReady();
                                return false;
                            }
                        })
                        .into(imageView);
            }

            @Override
            public void displayGifImage(@NonNull Fragment context, @NonNull String path, ImageView imageView, @NonNull final MySimpleTarget simpleTarget) {
                Glide.with(context)
                        .asGif()
                        .load(path)
                        .listener(new RequestListener<GifDrawable>() {
                            @Override
                            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<GifDrawable> target, boolean isFirstResource) {
                                simpleTarget.onLoadFailed(null);
                                return false;
                            }

                            @Override
                            public boolean onResourceReady(GifDrawable resource, Object model, Target<GifDrawable> target, DataSource dataSource, boolean isFirstResource) {
                                simpleTarget.onResourceReady();
                                return false;
                            }
                        })
                        .into(imageView);

            }

            @Override
            public void onStop(@NonNull Fragment context) {
                Glide.with(context).onStop();
            }

            @Override
            public void clearMemory(@NonNull Context c) {
                Glide.get(c).clearMemory();
            }
        };
        ZoomMediaLoader.getInstance().init(zoomMediaLoader);
        GPreviewBuilder.from(activity)
                .setData(imageBeans)//集合
                .setCurrentIndex(position)
                .setSingleFling(false)//是否在黑屏区域点击返回
                .setDrag(false)//是否禁用图片拖拽返回
                .setType(GPreviewBuilder.IndicatorType.Dot)//指示器类型
                .start();//启动
    }



    public static void preview2(Activity activity, List<String> imagePaths, int position){
        IZoomMediaLoader zoomMediaLoader=new IZoomMediaLoader(){
            @Override
            public void displayImage(@NonNull Fragment context, @NonNull String path, ImageView imageView, @NonNull final MySimpleTarget simpleTarget) {
                Glide.with(context)
                        .asBitmap()
                        .load(path)
                        .listener(new RequestListener<Bitmap>() {
                            @Override
                            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                                simpleTarget.onLoadFailed(null);
                                return false;
                            }

                            @Override
                            public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                                simpleTarget.onResourceReady();
                                return false;
                            }
                        })
                        .into(imageView);
            }

            @Override
            public void displayGifImage(@NonNull Fragment context, @NonNull String path, ImageView imageView, @NonNull final MySimpleTarget simpleTarget) {
                Glide.with(context)
                        .asGif()
                        .load(path)
                        .listener(new RequestListener<GifDrawable>() {
                            @Override
                            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<GifDrawable> target, boolean isFirstResource) {
                                simpleTarget.onLoadFailed(null);
                                return false;
                            }

                            @Override
                            public boolean onResourceReady(GifDrawable resource, Object model, Target<GifDrawable> target, DataSource dataSource, boolean isFirstResource) {
                                simpleTarget.onResourceReady();
                                return false;
                            }
                        })
                        .into(imageView);

            }

            @Override
            public void onStop(@NonNull Fragment context) {
                Glide.with(context).onStop();
            }

            @Override
            public void clearMemory(@NonNull Context c) {
                Glide.get(c).clearMemory();
            }
        };
        List<PreImageBean> preImageBeanList=new ArrayList<>();
        for (String path:imagePaths){
            PreImageBean bean=new PreImageBean(path);
            preImageBeanList.add(bean);
        }
        ZoomMediaLoader.getInstance().init(zoomMediaLoader);
        GPreviewBuilder.from(activity)
                .setData(preImageBeanList)//集合
                .setCurrentIndex(position)
                .setSingleFling(false)//是否在黑屏区域点击返回
                .setDrag(false)//是否禁用图片拖拽返回
                .setType(GPreviewBuilder.IndicatorType.Dot)//指示器类型
                .start();//启动
    }

}


//GPreviewBuilder.from(GridViewCustomActivity.this)//activity实例必须
//        .to(CustomActivity.class)//自定义Activity 使用默认的预览不需要
//        .setData(mThumbViewInfoList)//集合
//        .setUserFragment(UserFragment.class)//自定义Fragment 使用默认的预览不需要
//        .setCurrentIndex(position)
//        .setSingleFling(false)//是否在黑屏区域点击返回
//        .setDrag(false)//是否禁用图片拖拽返回
//        .setType(GPreviewBuilder.IndicatorType.Dot)//指示器类型
//        .start();//启动